// =============================================================
// CScript
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Jul. 10, 2006
//----------------------------------------------------------------------
#include "CScript.h"
//----------------------------------------------------------------------
CScript::CScript()
{
	// set environment variables for development environment
	// -----------------------------------------------
	// use malloc() to solve potential error of putenv()
	// -----------------------------------------------
	global_environment_variable = (char *)malloc(2048);
}
//----------------------------------------------------------------------
CScript::~CScript()
{
	free(global_environment_variable);
}
//----------------------------------------------------------------------
string &CScript::replaceText(string &text, const char *pttr, const char *new_val)
{
	string ttext = text;
	string tstr = pttr;
	string rstr = new_val;

	int n = ttext.length();
	int start, stop;
	int tstrlen = tstr.length();

	start = 0;
	while((start>=0)&&(start<n)) {
		stop = ttext.find(tstr,start);
		if((stop<0)||(stop>n)) break;

		string tmp = ttext.substr(0,stop);
		tmp += rstr;
		//tmp += ttext.substr(stop+tstrlen,n);
		tmp += ttext.substr(stop+tstrlen);

		ttext = tmp;
		n = ttext.length();
		//start = stop+tstrlen;
		start = 0;
	}
								    
	text = ttext;
	return text;
}
//----------------------------------------------------------------------
string &CScript::replaceVariable(string &text, string &pttr, string &new_val)
{
	string vname = pttr;

	if(vname[0]=='@') vname = vname.substr(1);

	//string variable1 = "$" + vname;
	string variable2 = "${" + vname + "}";
	//string variable3 = "$(" + vname + ")";

	// substitute them to their results after executing immediate commands
	string begin_mark = "^orz^";
	text = replaceImmediateCommand(text, begin_mark);
	new_val = replaceImmediateCommand(new_val, begin_mark);

	// hmmm.. attention to the order
	text = replaceText(text, variable2.c_str(), new_val.c_str());
	//text = replaceText(text, variable3.c_str(), new_val.c_str());
	//text = replaceText(text, variable1.c_str(), new_val.c_str());

	return text;
}
//----------------------------------------------------------------------
string &CScript::replaceImmediateCommand(string &imm_cmd, string &begin_mark)
{
	int n = imm_cmd.length();
	int pos = imm_cmd.find("^orz^");
	if((pos>=0)&&(pos<n)) {
		string immediate_cmd = imm_cmd.substr(pos+5,n);

		string result = executeShellCommand(immediate_cmd);

		imm_cmd = result;
	}

	return imm_cmd;
}
//----------------------------------------------------------------------
string &CScript::executeShellCommand(string &command)
{
	char buf[1024];
  	memset((char *)buf, '\0', sizeof(buf));

	FILE *p = popen(command.c_str(), "r");
	if(!p) {
		cout << "pipe open error : " << command << endl;
 	}

	// to contain the result of the command execution
	string tmp = "";
	while(fgets(buf, sizeof(buf)-1, p)!=0) {
		tmp += buf;
		memset((char *)buf, '\0', sizeof(buf));
	}

	pclose(p);

	string result;
	int len = tmp.length();
	if(tmp[len-1]=='\n') result = tmp.substr(0,tmp.length()-1);
	else result = tmp;

	command = result;

	return command;
}
//----------------------------------------------------------------------
